Results

source("codes/utils.R")
load("data/settings.RData")
load("data/cluster_fit.RData")
theme_set(
  theme_minimal() + 
    theme(
      legend.position = "bottom"
    )
)
fit_df %>% 
  transmute(time, k,  silhuette = map_dbl(fit, "criterion")) %>% 
  ggplot() + 
  aes(k , time, fill = silhuette) + 
  geom_tile(color = "black")  +
  scale_x_continuous(breaks = 2:10, labels = as.integer, name = "# clusters")
h_df %>% 
  pivot_longer(-(1:2)) %>% 
  ggplot() + 
  aes(time, value, color = as.factor(clust)) + 
  facet_wrap(~ name) + 
  geom_line() +
  labs(color = "Cluster", y = "Scaled center")
h_rescaled_df %>% 
  pivot_longer(-(1:2)) %>% 
  ggplot() + 
  aes(time, value, color = as.factor(clust)) + 
  facet_wrap(~ name, scales = "free_y") + 
  geom_line() +
  labs(color = "Cluster", y = "Center")
u_df %>% 
  pivot_longer(starts_with("cl")) %>% 
  group_by(time, geo) %>% 
  slice_max(value) %>% 
  mutate(clus = str_remove_all(name, "\\D")) %>% 
  full_join(euro_map) %>% 
  group_by(time) %>% 
  filter(!is.na(time)) %>% 
  group_walk(.keep = TRUE, ~ {
    message(.$time[[1]])
    p <- ggplot(., aes(geometry = geometry, fill = clus)) + 
      scale_x_continuous(limits = c(-10, 33)) +
      scale_y_continuous(limits = c(35, 65)) +
      geom_sf(color = "black", na.rm = FALSE) + 
      ggtitle(.$time[[1]])
    print(p)
  })
## Joining, by = "geo"
## 1999
## 2000

## 2001

## 2002

## 2003

## 2004

## 2005

## 2006

## 2007

## 2008

## 2009

## 2010

## 2011

## 2012

## 2013

## 2014

## 2015

## 2016

## 2017

## 2018

## 2019
u_df %>% 
  group_by(geo) %>% 
  slice(1, n()) %>% 
  ungroup() %>% 
  select(time:geo, starts_with("cl")) %>% 
  pivot_longer(starts_with("cl"), names_to = "clus", values_to = "u") %>% 
  mutate(
    clus = str_remove_all(clus, "\\D"),
    l = ifelse(u > .15, u, NA)
  ) %>% 
  group_by(country) %>% 
  group_walk(.keep = T, ~ {
    p <- ggplot(.) + 
      aes(u, geo, fill = clus) + 
      facet_wrap(~ time) + 
      geom_col(color = "black") + 
      geom_text(aes(label = scales::percent(l, accuracy = .1)), position = position_fill(vjust = .5)) +
      ggtitle(countrycode::countrycode(.$country[[1]], "iso2c", "country.name"))
    
    print(p)
  }
  )

u_df %>% 
  pivot_longer(starts_with("cl"), names_to = "clus", values_to = "u") %>% 
  mutate(clus = str_remove_all(clus, "\\D")) %>% 
  group_by(country) %>% 
  group_walk(.keep = TRUE, ~ {
    p <- ggplot(., aes(time, u, fill = clus)) + 
      facet_wrap(~ geo) + 
      geom_area(color = "black")  + 
      ggtitle(.$country[[1]])
    print(p)
  })